/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.tyf.common.mapper;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tyf.common.annotation.ApiFetch;
import com.tyf.common.util.CommonUtils;
import com.tyf.common.log.LogUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.dozer.DozerBeanMapper;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
 * <p>
 * 1. 持有Mapper的单例.
 * 2. 返回值类型转换.
 * 3. 批量转换Collection中的所有对象.
 * 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
 *
 * @author calvin
 */
public class BeanMapper {

    /**
     * 持有Dozer单例, 避免重复创建DozerMapper消耗资源.
     */
    private static DozerBeanMapper dozer = new DozerBeanMapper();

    /**
     * 基于Dozer转换对象的类型.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return dozer.map(source, destinationClass);
    }

    /**
     * list对象转换
     * 基于Dozer转换Collection中对象的类型.
     */
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    /**
     * list对象转换 && 增加lambada表达式，可处理转换后对象的内容
     * 基于Dozer转换Collection中对象的类型.
     *
     * @Code SystemUser u1 = BizUtils.getDefaultUser();
     * List<SystemUser> list = Lists.newArrayList();
     * list.add(u1);
     * BeanMapper.mapList(list, User.class, m -> {
     * System.out.println("generate lambda");
     * m.setUserName("=====lambda");
     * System.out.println("======" + m.getUserName());
     * });
     */
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass, Consumer<? super T> action) {
        StopWatch clock = new StopWatch();
        clock.start();

        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
            action.accept(destinationObject);
        }

        LogUtils.logInfo("mapList costs " + clock.getTime() + "ms");
        return destinationList;
    }


    /**
     * 基于Dozer将对象A的值拷贝到对象B中.
     */
    public static void copy(Object source, Object destinationObject) {
        dozer.map(source, destinationObject);
    }

    /**
     * list对象转换增强
     * 基于Dozer转换Collection中对象的类型 && 处理destinationClass中的apiFetch外关联属性
     */
    public static <T> List<T> mapListPlus(Collection sourceList, Class<T> destinationClass) {
        return mapListPlus(sourceList, destinationClass, null);
    }

    /**
     * list对象转换增强  & 增加lambada表达式，可处理转换后对象的内容
     * 基于Dozer转换Collection中对象的类型 && 处理destinationClass中的apiFetch外关联属性
     */
    public static <T> List<T> mapListPlus(Collection sourceList, Class<T> destinationClass, Consumer<? super T> action) {
        StopWatch clock = new StopWatch();
        clock.start();

        // 获取ApiFetch的注解元数据Map
        Map<ApiFetch, String> metaData = CommonUtils.getApiFectchMetaData(destinationClass);
        // 本地缓存，所做的优化，有些业务外关联数据只能使用本地缓存进行优化
        Map<String, Object> localCaches = Maps.newHashMap();
        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
            // 增加lambada表达式，可处理转换后对象的内容
            if (action != null) {
                action.accept(destinationObject);
            }
        }

        LogUtils.logInfo("mapListPlus costs " + clock.getTime() + "ms");
        return destinationList;
    }

}